package com.szbber.medical.board.ui.fragment

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.orhanobut.logger.Logger
import com.szbber.commen.base.BaseVMFragment
import com.szbber.commen.bean.PatientListBean
import com.szbber.medical.board.R
import com.szbber.medical.board.databinding.BoardFragmentPatientlistBinding
import com.szbber.medical.board.databinding.BoardItemPatientlistBinding
import com.szbber.medical.board.ui.activity.PatientDetalisActivity
import com.szbber.medical.board.viewModel.PatientInfoViewModel
import kotlinx.android.synthetic.main.board_fragment_patientlist.*

class PatientInfoFragment : BaseVMFragment<BoardFragmentPatientlistBinding, PatientInfoViewModel> (){

    companion object {
        fun getInstance(): PatientInfoFragment {
            return PatientInfoFragment()
        }
    }


    override fun getLayoutResId(): Int = R.layout.board_fragment_patientlist

    override fun initVM(): PatientInfoViewModel = ViewModelProvider(this,
        ViewModelProvider.NewInstanceFactory()).get(PatientInfoViewModel::class.java)

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.patientlistVm = mViewModel
        mViewModel.getPatientList(activity)
        observeRecyclerView()
    }

    private fun observeRecyclerView(){
        mViewModel.patientListBean.observe(this) {
            val adapter = object : BaseQuickAdapter<PatientListBean.Data, BaseViewHolder>(
                R.layout.board_item_patientlist, it.data.toMutableList()
            ) {
                override fun convert(holder: BaseViewHolder, item: PatientListBean.Data) {
                    val binding =
                        DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as BoardItemPatientlistBinding
                    binding.itempatientlist = item
                    item.nurseLevel?.bg_color?.apply {
                        binding.rlTop.setBackgroundColor(Color.parseColor(this))
                    }

                    binding.cvItem.setOnClickListener {
                        val intent = Intent(activity!!, PatientDetalisActivity::class.java)
                        intent.putExtra("data", item.cure_no)
                        startActivity(intent)
                    }
                    binding.executePendingBindings()
                }
            }
            val manager = GridLayoutManager(activity,5)
            mRecyclerView.adapter = adapter
            mRecyclerView.layoutManager = manager
        }
    }

}